package com.ff.exerpro.controller;

import com.ff.exerpro.bean.Emp;
import com.ff.exerpro.common.R;
import com.ff.exerpro.service.EmpService;
import com.ff.exerpro.vo.req.EmpAddVo;
import com.ff.exerpro.vo.req.EmpUpdateVo;
import com.ff.exerpro.vo.resp.EmpRespVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@PropertySource("classpath:config/ui.properties")
@CrossOrigin
@RequestMapping("/api/v1")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/employee/{id}")
    public R<EmpRespVo> getEmpById(@PathVariable int id) {
        Emp emp = empService.getEmpById(id);
        EmpRespVo empRespVo = new EmpRespVo();
        BeanUtils.copyProperties(emp, empRespVo);

        return R.success(empRespVo);
    }


    @Value("${ui.emp.page.size}")
    private int pageSize;

    @GetMapping("/employees/page")
    public PageInfo getEmpPageInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Emp> empList = empService.getAllEmp();

//        List<EmpRespVo> empRespVoList = new ArrayList<>();
//        for (Emp emp : empList) {
//            EmpRespVo empRespVo = new EmpRespVo();
//            BeanUtils.copyProperties(emp, empRespVo);
//            empRespVoList.add(empRespVo);
//        }

        List<EmpRespVo> collect = empList.stream()
                .map(employee -> {
                    EmpRespVo vo = new EmpRespVo();
                    BeanUtils.copyProperties(employee, vo);
                    return vo;
                }).collect(Collectors.toList());
        return new PageInfo<>(collect);
    }

    @Transactional
    @PostMapping("/employee")
    public R addOneEmp(@RequestBody @Valid EmpAddVo empAddVo) {
        // vo要转为do,这一步不要再忘
        Emp emp = new Emp();
        BeanUtils.copyProperties(empAddVo, emp);
        empService.addOneEmp(emp);
        return R.success();
    }

    @Transactional
    @PostMapping("/employees")
    public R addSomeEmps(@RequestBody @Valid List<EmpAddVo> emps) {
//        List<Emp> empList = new ArrayList<>();
//        for (EmpAddVo empAddVo : emps) {
//            Emp emp = new Emp();
//            BeanUtils.copyProperties(empAddVo, emp);
//            empList.add(emp);
//        }

        List<Emp> collect = emps.stream()
                        .map( employee->{
                            Emp emp = new Emp();
                            BeanUtils.copyProperties(employee, emp);
                            return emp;
                        }).collect(Collectors.toList());
        empService.addSomeEmps(collect);
        return R.success();
    }

    @Transactional
    @PutMapping("/employee")
    public R updateEmp(@RequestBody EmpUpdateVo updateVo) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(updateVo, emp);

        empService.updateOneEmp(emp);
        return R.success();
    }

    @Transactional
    @PutMapping("/employees")
    public R updateEmps(@RequestBody List<EmpUpdateVo> updateVo) {
        List<Emp> emps = updateVo.stream().map(employee->{
            Emp emp = new Emp();
            BeanUtils.copyProperties(employee, emp);
            return emp;
        }).collect(Collectors.toList());
        empService.updateSomeEmps(emps);
        return R.success();
    }

    @GetMapping("/lunch")
    public String getLunch() {
        return "该吃午饭了";
    }
}
